/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.dromara.hmily.demo.springcloud.account.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.log4j.Log4j2;
import org.dromara.hmily.annotation.HmilyTCC;
import org.dromara.hmily.common.exception.HmilyRuntimeException;
import org.dromara.hmily.demo.springcloud.account.dto.AccountDTO;
import org.dromara.hmily.demo.springcloud.account.entity.AccountEntity;
import org.dromara.hmily.demo.springcloud.account.mapper.AccountMapper;
import org.dromara.hmily.demo.springcloud.account.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;

/**
 * The type Account service.
 *
 * @author xiaoyu
 */
@Service("accountService")
@Log4j2
public class AccountServiceImpl implements AccountService {

    /**
     * log.
     */

    private final AccountMapper accountMapper;

    /**
     * Instantiates a new Account service.
     *
     * @param accountMapper the account mapper
     */
    @Autowired(required = false)
    public AccountServiceImpl(final AccountMapper accountMapper) {
        this.accountMapper = accountMapper;
    }

    /**
     * 分布式事务出错时调用cancel，成功时调用confirm
     *
     * @param accountDTO 参数dto
     */
    @Override
    @HmilyTCC(confirmMethod = "confirm", cancelMethod = "cancel")
    @Transactional(rollbackFor = Exception.class)
    public void payment(final AccountDTO accountDTO) {
        System.out.println("=======执行扣款开始===========");
        QueryWrapper<AccountEntity> qw = new QueryWrapper<>();
        qw.lambda().eq(AccountEntity::getUserId, accountDTO.getUserId());
        AccountEntity oldAccount = accountMapper.selectOne(qw);
        if (Objects.isNull(oldAccount)) {
            throw new HmilyRuntimeException("用户不存在");
        }
        oldAccount.setMoney(oldAccount.getMoney() - accountDTO.getMoney());
        accountMapper.updateById(oldAccount);
//        int i=1/0;
    }

    /**
     * Confirm boolean.
     *
     * @param accountDTO the account dto
     * @return the boolean
     */
    public void confirm(final AccountDTO accountDTO) {
        log.debug("============执行扣款成功===============");
    }


    /**
     * 分布式出错时调用该方法，进行按照业务逻辑的回滚代码编写
     */
    public void cancel(final AccountDTO accountDTO) {
        log.debug("============执行扣款失败,根据业务回滚代码===============");
        QueryWrapper<AccountEntity> qw = new QueryWrapper<>();
        qw.lambda().eq(AccountEntity::getUserId, accountDTO.getUserId());
        AccountEntity oldAccount = accountMapper.selectOne(qw);
        if (!Objects.isNull(oldAccount)) {
            oldAccount.setMoney(oldAccount.getMoney() + accountDTO.getMoney());
            accountMapper.updateById(oldAccount);
        }
    }
}
